package control
{
	import com.greensock.TweenMax;
	
	import events.AddMouseTouchEvent;
	import events.MouseTouchEvent;
	import events.MyEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class ScrollPicsBanner_hor extends EventDispatcher
	{
		protected var mouseEventHolder:AddMouseTouchEvent;
		protected var rootEventHolder:AddMouseTouchEvent;
		protected var startMouseX:Number;
		protected var startX:Number;
		protected var endMouseX:Number;
		protected var target:DisplayObject;
		protected var distance:Number=0;
		protected var area:Rectangle;
		protected var finalX:Number;
		protected var _run:Boolean=true;
		public function ScrollPicsBanner_hor(_target:DisplayObject,_area:Rectangle,ie:IEventDispatcher=null)
		{
			super(ie);
			target=_target;
			area=_area;
			
			
			mouseEventHolder=new AddMouseTouchEvent(target);
			mouseEventHolder.addEventListener(mouseEventHolder.PRESS_DOWN,handleBannerMouseDown,false,0,true);
			rootEventHolder=new AddMouseTouchEvent(target.root.stage);
		}
		
		
		public function get run():Boolean
		{
			return _run;
		}

		public function set run(value:Boolean):void
		{
			_run = value;
		}

		protected function handleBannerMouseDown(e:MouseTouchEvent):void
		{
			if(!run)return;
			this.dispatchEvent(new MyEvent("moved", null, true));
			startMouseX = e.stageX;
			startX=target.x;
			rootEventHolder.addEventListener(mouseEventHolder.PRESS_UP, handleBannerMouseUp,false,0,true);
			rootEventHolder.addEventListener(mouseEventHolder.PRESS_MOVE, handleBannerMouseMove,false,0,true);
		}
		
		protected function handleBannerMouseMove(e:MouseTouchEvent):void
		{
			if(!run)
			{
				handleBannerMouseUp();
				return;
			}
			this.dispatchEvent(new MyEvent("moved", null, true));
			endMouseX = e.stageX
			
			distance = startMouseX - endMouseX;
			moveFunction();
		}
		
		protected function moveFunction():void
		{
			
			if(!run)return;
			var _target:Number = startX - distance;
			if(startX>_target)
			{
				if (_target < area.x + area.width - target.width) 
				{
					_target=area.x + area.width - target.width-100;
				}
			}
			else if(startX<_target)
			{
				if (_target > area.x )
				{
					_target = area.x+100;
				}
				
			}
			TweenMax.to(target, 0.5, { x:_target} );
			if (_target < area.x + area.width - target.width) _target = area.x + area.width - target.width;
			if (_target > area.x )_target = area.x;
			finalX=_target;
		}
		
		public function back():void
		{
			TweenMax.to(target,0.5,{x:finalX});
		}
		
		protected function handleBannerMouseUp(e:MouseTouchEvent=null):void
		{
			
			endMouseX = e.stageX;
			distance = startMouseX - endMouseX;
			
			back();
			rootEventHolder.removeEventListener(mouseEventHolder.PRESS_UP, handleBannerMouseUp);
			rootEventHolder.removeEventListener(mouseEventHolder.PRESS_MOVE, handleBannerMouseMove);
			
		}
		
		public function gc():void
		{
			rootEventHolder.removeAllEvent();
			mouseEventHolder.removeAllEvent();
			mouseEventHolder.removeEventListener(mouseEventHolder.PRESS_DOWN,handleBannerMouseDown);
		}
		
	}
}